//
//  ZCFGV2TableViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/4.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ZCFGV2TableViewModel.h"

#import "ZCFGCellModel.h"

@interface ZCFGV2TableViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation ZCFGV2TableViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    /// viewDidLoad后加载数据
//    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 请求数据
    self.requestDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id inputInfo) {
        
        @strongify(self);
        
        int loadIndex = [inputInfo[@"LoadIndex"] intValue];
        
        return [self requestListDataSignalWithPageIndex:loadIndex pageSize:List_PageSize];
    }];
    
    /// 选中
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(ZCFGCellModel * cellModel)
    {
        ArticleListData* aListData = cellModel.data;
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
                [subscriber sendNext:aListData];
                [subscriber sendCompleted];
            });
            return nil;
        }];
    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
}

- (RACSignal *)requestListDataSignalWithPageIndex:(int)pageIndex pageSize:(int)pageSize
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_getcatelist params:interface_param_getcatelist(self.cateID, self.areaID, @(pageIndex+1), @(pageSize))] subscribeNext:^(id x)
         {
             @strongify(self);
             
             ArticlesListData* data = [ArticlesListData modelWithJSON:x];
             
             @weakify(self);
             NSArray *viewModels = [data.datas.rac_sequence map:^ZCFGCellModel *(ArticleListData* aListData)
                                    {
                                        @strongify(self);
                                        ZCFGCellModel *itemViewModel = [[ZCFGCellModel alloc] initWithData:aListData cellHeight:self.cellHeight];
                                        
                                        return itemViewModel;
                                    }].array;
             
             if (viewModels == nil || viewModels.count == 0)
             {
                 self.lastPage = self.page;
             }
             else
             {
                 self.lastPage = 999;
             }
             
             if (self.shouldPullUpToLoadMore)
             {
                 if (0 == pageIndex)
                 {
                     self.dataSource = viewModels;
                 }
                 else
                 {
                     if (self.dataSource == nil)
                     {
                         self.dataSource = viewModels;
                     }
                     else
                     {
                         self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                     }
                 }
             }
             else
             {
                 self.dataSource = viewModels;
             }
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end
